package com.hd.assistant.web.common.base.ordermining;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.hd.assistant.biz.manager.AssistantManager;
import com.hd.assistant.biz.manager.exception.ManagerException;
import com.hd.assistant.biz.service.PaiPaiIsvService;
import com.hd.assistant.biz.service.exception.ServiceException;
import com.hd.assistant.biz.util.PaipaiUtil;
import com.hd.assistant.dal.dataobject.User;



public class BasePaipaiOrderModule {
	public static String APP_TYPE= "appType";

	
	@Autowired
	protected AssistantManager assistantManager;
	

	@Autowired
	protected  PaiPaiIsvService paiPaiIsvService;

	protected static String PAIPAI_SESSION_KEY = "PU_1";
	protected User checkLogin(HttpSession session,String type){
		if(type.equals(User.Type_PAIPAI_ORDER)){
			Object o = session.getAttribute(PAIPAI_SESSION_KEY);
			User u = (User)o;
			return u;
		}
		return null;
	}

	protected void supplyUserInfo(User u,Context context) throws ServiceException{
	
}

	
	protected User getUserInfo(Long uid,String type) throws ManagerException {
		return assistantManager.getUserByUserIdAndType(uid, type);
	}
	
	protected void storeUserInfo(User u) throws ManagerException {
			assistantManager.insertUser(u);
		
	}
	
	protected void updateUserInfo(User u) throws ManagerException {
			assistantManager.updateUser(u);
	}
	
	public String getAppType(HttpSession session){
		//return (String)session.getAttribute(APP_TYPE);
		return User.Type_PAIPAI_ORDER;
	}
	protected User checkPermision(String appType, HttpSession session,Context context,Navigator nav) {
    	if(appType == null)
    		appType = getAppType(session);
    	context.put(APP_TYPE,appType);
    	
    	
    	if(appType!=null  && appType.equals(User.Type_PAIPAI_ORDER)){
    		User user = checkLogin(session,User.Type_PAIPAI_ORDER);
        	if(user == null){
        		nav.redirectToLocation(PaipaiUtil.getRedirectLoginUrl(PaipaiUtil.APP_OAUTH_ID_ORDER));
        	}else{
        		context.put("user", user);
        		return user;
        	}
    	}
    	return null;
	}
	


}
